export default {
  initLogic(list, baseTags) {
    let target = []
    for (let i = 0; i < list.length; i++) {
      if (list[i].labs && list[i].labs.length) {
        // let tag = baseTags.find(item => item.assetCode === list[i].labs[0].labId)
        target[i] = {
          logicalOperation: list[i].logicTyp,
          tagList: []
        }
        for (let j = 0; j < list[i].labs.length; j++) {
          if (list[i].labs[j].labs && list[i].labs[j].labs.length) {
            target[i].tagList[j] = {
              logicalOperation: list[i].labs[j].logicTyp,
              tagList: []
            }
            for (let k = 0; k < list[i].labs[j].labs.length; k++) {
              let tag = baseTags.find(item => item.assetCode === list[i].labs[j].labs[k].labId)
              target[i].tagList[j].tagList.push({
                tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
                tagNm: tag.assetNm,
                valueType: tag.valueType.toString(),
                valueTypeCode: tag.valueTypeCode,
                tagCode: list[i].labs[j].labs[k].labId,
                operationType: list[i].labs[j].labs[k].oprator,
                operationValue: list[i].labs[j].labs[k].valArray.length
                  ? list[i].labs[j].labs[k].valArray
                  : list[i].labs[j].labs[k].valLft
                  ? [list[i].labs[j].labs[k].valLft, list[i].labs[j].labs[k].valRit]
                  : list[i].labs[j].labs[k].val,
                enums: tag.enums,
                labTbl: list[i].labs[j].labs[k].labTbl,
                labFldMap: list[i].labs[j].labs[k].labFldMap,
                dictDatabaseType: list[i].labs[j].labs[k].datTyp,
                isDerLabel: list[i].labs[j].labs[k].isDerLabel || '0'
              })
            }
          } else {
            let tag = baseTags.find(item => item.assetCode === list[i].labs[j].labId)
            target[i].tagList[j] = {
              tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
              tagNm: tag.assetNm,
              valueType: tag.valueType.toString(),
              valueTypeCode: tag.valueTypeCode,
              tagCode: list[i].labs[j].labId,
              operationType: list[i].labs[j].oprator,
              operationValue: list[i].labs[j].valArray.length
                ? list[i].labs[j].valArray
                : list[i].labs[j].valLft
                ? [list[i].labs[j].valLft, list[i].labs[j].valRit]
                : list[i].labs[j].val,
              enums: tag.enums,
              labTbl: list[i].labs[j].labTbl,
              labFldMap: list[i].labs[j].labFldMap,
              dictDatabaseType: list[i].labs[j].datTyp,
              isDerLabel: list[i].labs[j].isDerLabel || '0'
            }
          }
        }
      } else {
        let tag = baseTags.find(item => item.assetCode === list[i].labId)
        target[i] = {
          tagId: Number(tag.assetId) ? tag.assetId : Number(tag.assetId),
          tagNm: tag.assetNm,
          valueType: tag.valueType.toString(),
          valueTypeCode: tag.valueTypeCode,
          tagCode: list[i].labId,
          operationType: list[i].oprator,
          operationValue: list[i].valArray.length
            ? list[i].valArray
            : list[i].valLft
            ? [list[i].valLft, list[i].valRit]
            : list[i].val,
          enums: tag.enums,
          labTbl: list[i].labTbl,
          labFldMap: list[i].labFldMap,
          dictDatabaseType: list[i].datTyp,
          isDerLabel: list[i].isDerLabel || '0'
        }
      }
    }
    return target
  },
  initParams(formData, grpFlterData, drillGrps) {
    let derivedTag = {
      request: {
        dataDate: formData.dataDate,
        labGrps: {},
        grpFlter: []
      },
      extra: {},
      cache: {}
    }
    if (drillGrps && drillGrps.length) {
      derivedTag.request.drillGrps = drillGrps
      drillGrps.forEach(drillArray => {
        drillArray.forEach(drill => {
          if (!Object.keys(derivedTag.cache).find(label => label === drill.mainDrill.labId)) {
            derivedTag.cache[drill.mainDrill.labId] = {
              tagNm: drill.mainDrill.tagNm
            }
          }
          if (drill.subDrill) {
            if (!Object.keys(derivedTag.cache).find(label => label === drill.subDrill.labId)) {
              derivedTag.cache[drill.subDrill.labId] = {
                tagNm: drill.subDrill.tagNm
              }
            }
          }
        })
      })
    }
    let tagList = formData.derivedTag.tagList
    if (tagList.length) {
      derivedTag.request.labGrps = {
        labs: [],
        level: 1,
        logicTyp: formData.derivedTag.logicalOperation || 'and'
      }
      for (let i = 0; i < tagList.length; i++) {
        if (tagList[i].tagList) {
          derivedTag.request.labGrps.labs[i] = {
            labs: [],
            level: 2,
            logicTyp: tagList[i].logicalOperation
          }
          for (let j = 0; j < tagList[i].tagList.length; j++) {
            if (tagList[i].tagList[j].tagList) {
              derivedTag.request.labGrps.labs[i].labs[j] = {
                labs: [],
                level: 3,
                logicTyp: tagList[i].tagList[j].logicalOperation
              }
              for (let k = 0; k < tagList[i].tagList[j].tagList.length; k++) {
                var target = tagList[i].tagList[j].tagList[k]
                let item = {
                  labTbl: target.labTbl,
                  oprator: target.operationType,
                  datTyp: target.dictDatabaseType,
                  logicTyp: 'and',
                  labFldMap: target.labFldMap,
                  dtTyp: target.valueType == '4' ? 'yyyyMMdd' : target.valueType == '5' ? 'yyyyMMdd HH:mm:ss' : '',
                  labId: target.tagCode,
                  isDerLabel: target.isDerLabel
                }
                if (target.valueType == '1') {
                  item.val = ''
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = target.operationValue
                } else if (
                  target.valueType == '4' ||
                  target.valueType == '5' ||
                  target.valueType == '6' ||
                  target.valueType == '7'
                ) {
                  if (target.operationType === 'a<=x<b') {
                    item.val = ''
                    item.valLft = target.operationValue[0]
                    item.valRit = target.operationValue[1]
                    item.valArray = []
                  } else {
                    item.val = target.operationValue
                    item.valLft = ''
                    item.valRit = ''
                    item.valArray = []
                  }
                } else {
                  item.val = target.operationValue
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = []
                }
                derivedTag.request.labGrps.labs[i].labs[j].labs[k] = item
                if (!Object.keys(derivedTag.cache).find(label => label === target.tagCode)) {
                  derivedTag.cache[target.tagCode] = {
                    tagNm: target.tagNm
                  }
                }
              }
            } else {
              var target2 = tagList[i].tagList[j]
              let item = {
                labTbl: target2.labTbl,
                oprator: target2.operationType,
                datTyp: target2.dictDatabaseType,
                logicTyp: 'and',
                labFldMap: target2.labFldMap,
                dtTyp: target2.valueType == '4' ? 'yyyyMMdd' : target2.valueType == '5' ? 'yyyyMMdd HH:mm:ss' : '',
                labId: target2.tagCode,
                isDerLabel: target2.isDerLabel
              }
              if (target2.valueType == '1') {
                item.val = ''
                item.valLft = ''
                item.valRit = ''
                item.valArray = target2.operationValue
              } else if (
                target2.valueType == '4' ||
                target2.valueType == '5' ||
                target2.valueType == '6' ||
                target2.valueType == '7'
              ) {
                if (target2.operationType === 'a<=x<b') {
                  item.val = ''
                  item.valLft = target2.operationValue[0]
                  item.valRit = target2.operationValue[1]
                  item.valArray = []
                } else {
                  item.val = target2.operationValue
                  item.valLft = ''
                  item.valRit = ''
                  item.valArray = []
                }
              } else {
                item.val = target2.operationValue
                item.valLft = ''
                item.valRit = ''
                item.valArray = []
              }
              derivedTag.request.labGrps.labs[i].labs[j] = item
              if (!Object.keys(derivedTag.cache).find(label => label === target2.tagCode)) {
                derivedTag.cache[target2.tagCode] = {
                  tagNm: target2.tagNm
                }
              }
            }
          }
        } else {
          let item = {
            labTbl: tagList[i].labTbl,
            oprator: tagList[i].operationType,
            datTyp: tagList[i].dictDatabaseType,
            logicTyp: 'and',
            labFldMap: tagList[i].labFldMap,
            dtTyp: tagList[i].valueType == '4' ? 'yyyyMMdd' : tagList[i].valueType == '5' ? 'yyyyMMdd HH:mm:ss' : '',
            labId: tagList[i].tagCode,
            isDerLabel: tagList[i].isDerLabel
          }
          if (tagList[i].valueType == '1') {
            item.val = ''
            item.valLft = ''
            item.valRit = ''
            item.valArray = tagList[i].operationValue
          } else if (
            tagList[i].valueType == '4' ||
            tagList[i].valueType == '5' ||
            tagList[i].valueType == '6' ||
            tagList[i].valueType == '7'
          ) {
            if (tagList[i].operationType === 'a<=x<b') {
              item.val = ''
              item.valLft = tagList[i].operationValue[0]
              item.valRit = tagList[i].operationValue[1]
              item.valArray = []
            } else {
              item.val = tagList[i].operationValue
              item.valLft = ''
              item.valRit = ''
              item.valArray = []
            }
          } else {
            item.val = tagList[i].operationValue
            item.valLft = ''
            item.valRit = ''
            item.valArray = []
          }
          derivedTag.request.labGrps.labs[i] = item
          if (!Object.keys(derivedTag.cache).find(label => label === tagList[i].tagCode)) {
            derivedTag.cache[tagList[i].tagCode] = {
              tagNm: tagList[i].tagNm
            }
          }
        }
        // if (tagList[i].derivedTag && tagList[i].derivedTag.tagList.length) {
        //   derivedTag.request.labGrps.labs.push({
        //     labs: [
        //       item,
        //       {
        //         labs: [],
        //         level: 3,
        //         logicTyp: tagList[i].derivedTag.logicalOperation
        //       }
        //     ],
        //     level: 2,
        //     logicTyp: 'and'
        //   })
        //   if (!Object.keys(derivedTag.cache).find(label => label === tagList[i].tagCode)) {
        //     derivedTag.cache[tagList[i].tagCode] = {
        //       tagNm: tagList[i].tagNm
        //     }
        //   }
        //   for (let j = 0; j < tagList[i].derivedTag.tagList.length; j++) {
        //     var target = tagList[i].derivedTag.tagList[j]
        //     let item2 = {
        //       labTbl: target.labTbl,
        //       oprator: target.operationType,
        //       datTyp: target.dictDatabaseType,
        //       logicTyp: 'and',
        //       labFldMap: target.labFldMap,
        //       dtTyp: target.valueType === '4' ? 'yyyyMMdd' : target.valueType === '5' ? 'yyyyMMdd HH:mm:ss' : '',
        //       labId: target.tagCode
        //     }
        //     if (target.valueType === '1') {
        //       item2.val = ''
        //       item2.valLft = ''
        //       item2.valRit = ''
        //       item2.valArray = target.operationValue
        //     } else if (
        //       target.valueType === '4' ||
        //       target.valueType === '5' ||
        //       target.valueType === '6' ||
        //       target.valueType === '7'
        //     ) {
        //       if (target.operationType === 'a<=x<b') {
        //         item2.val = ''
        //         item2.valLft = target.operationValue[0]
        //         item2.valRit = target.operationValue[1]
        //         item2.valArray = []
        //       } else {
        //         item2.val = target.operationValue
        //         item2.valLft = ''
        //         item2.valRit = ''
        //         item2.valArray = []
        //       }
        //     } else {
        //       item2.val = target.operationValue
        //       item2.valLft = ''
        //       item2.valRit = ''
        //       item2.valArray = []
        //     }
        //     derivedTag.request.labGrps.labs[i].labs[1].labs.push(item2)
        //     if (!Object.keys(derivedTag.cache).find(label => label === item2.labId)) {
        //       derivedTag.cache[item2.labId] = {
        //         tagNm: target.tagNm
        //       }
        //     }
        //   }
        // } else {
        //   derivedTag.request.labGrps.labs.push(item)
        //   if (!Object.keys(derivedTag.cache).find(label => label === item.labId)) {
        //     derivedTag.cache[item.labId] = {
        //       tagNm: tagList[i].tagNm
        //     }
        //   }
        // }
      }
    }
    grpFlterData.forEach((item, index) => {
      derivedTag.request.grpFlter.push({
        grpId: item.grpId || index,
        mainGrp: {
          labId: item.mainGrp.labId,
          groupTyp: item.mainGrp.groupTyp,
          labGrps: item.mainGrp.labGrps
        }
      })
      derivedTag.extra[item.grpId || index] = {
        [item.mainGrp.labId]: {
          groupName: item.mainGrp.groupName,
          groupTyp: item.mainGrp.groupTyp,
          graphType: item.mainGrp.graphType,
          labTbl: item.mainGrp.labTbl,
          labFldMap: item.mainGrp.labFldMap,
          rangeInfo: item.mainGrp.rangeInfo,
          valueType: item.mainGrp.valueType,
          enumType: item.mainGrp.enumType || '',
          enumVal: item.mainGrp.enumVal || ''
        }
      }
      if (!Object.keys(derivedTag.cache).find(label => label === item.mainGrp.labId)) {
        derivedTag.cache[item.mainGrp.labId] = {
          tagNm: item.mainGrp.tagNm
        }
      }
      if (item.subGrp && item.subGrp.labId) {
        const subIndex = derivedTag.request.grpFlter.findIndex(tag => tag.grpId === item.grpId)
        derivedTag.request.grpFlter[subIndex].subGrp = {
          labId: item.subGrp.labId,
          groupTyp: item.subGrp.groupTyp,
          labGrps: item.subGrp.labGrps
        }
        derivedTag.extra[item.grpId || index][item.subGrp.labId] = {
          groupName: item.subGrp.groupName,
          groupTyp: item.subGrp.groupTyp,
          graphType: item.subGrp.graphType,
          labTbl: item.subGrp.labTbl,
          labFldMap: item.subGrp.labFldMap,
          rangeInfo: item.subGrp.rangeInfo,
          valueType: item.subGrp.valueType
        }
        if (!Object.keys(derivedTag.cache).find(label => label === item.subGrp.labId)) {
          derivedTag.cache[item.subGrp.labId] = {
            tagNm: item.subGrp.tagNm
          }
        }
      } else {
        if ((item.mainGrp.valueType == '6' || item.mainGrp.valueType == '7') && item.numGrp) {
          derivedTag.request.grpFlter[index].numGrp = item.numGrp
        }
      }
    })
    return derivedTag
  }
}
